package com.platform.module.dao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.module.dao.entity.UserBonusTaskRecord;
import com.platform.module.dao.enums.UserBonusTaskVerifyEnum;
import com.platform.module.dao.enums.task.UserBonusTaskStateEnum;
import com.platform.module.dao.enums.task.UserBonusTaskTypeEnum;
import com.platform.module.dao.mapper.UserBonusTaskRecordMapper;
import com.platform.module.dao.service.UserBonusTaskRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
@Slf4j
public class UserBonusTaskRecordServiceImpl extends ServiceImpl<UserBonusTaskRecordMapper, UserBonusTaskRecord> implements UserBonusTaskRecordService {

    @Override
    public Boolean saveUserBonusTaskRecord(Long userId, Long activityConfigId, BigDecimal giftMoney, Integer cashoutMultiplier) {
        log.info("UserBonusTaskRecordServiceImpl.saveUserBonusTaskRecord userId[{}] activityConfigId[{}] giftMoney[{}] cashoutMultiplier[{}] -> 添加用户奖励任务", userId, activityConfigId, giftMoney,cashoutMultiplier);

        BigDecimal multiple = BigDecimal.valueOf(cashoutMultiplier);
        BigDecimal withdrawalBetAmount = multiple.multiply(giftMoney);

        UserBonusTaskRecord userBonusTaskRecord = new UserBonusTaskRecord();
        userBonusTaskRecord.setUserId(userId);
        userBonusTaskRecord.setTaskType(UserBonusTaskTypeEnum.REDEMPTION_CODE.getCode());
        userBonusTaskRecord.setState(UserBonusTaskStateEnum.PROGRESS.getCode());
        userBonusTaskRecord.setTaskAmount(giftMoney); // 获得的金额奖励
        userBonusTaskRecord.setCompletedAmount(BigDecimal.ZERO); // 进行中的金额，默认为0
        userBonusTaskRecord.setTargetAmount(withdrawalBetAmount); // 打码量
        userBonusTaskRecord.setMultiple(multiple); // 打码量倍数
        userBonusTaskRecord.setWeight(0);
        userBonusTaskRecord.setNote("Redeem Rewards: %s".formatted(giftMoney));
        userBonusTaskRecord.setCreateTime(LocalDateTime.now());
        userBonusTaskRecord.setEndTime(null);
        userBonusTaskRecord.setVerifyAmount(UserBonusTaskVerifyEnum.UN_VERIFY.getCode());
        userBonusTaskRecord.setPkid(String.valueOf(activityConfigId));
        return this.save(userBonusTaskRecord);
    }
}
